from bs4 import BeautifulSoup

if __name__ == '__main__':
    file = open(file="The Dormouse's story.html", mode="r", encoding="utf-8")
    html = file.read()
    file.close()

    # print(html)

    soup = BeautifulSoup(html, "html.parser")

    # # 1/选择 dom 中的 第一个 a 标签
    # taga = soup.a
    # # 打印完整的标签
    # print(taga)
    # # 打印标签名称
    # print(taga.name)
    # # 打印标签中的内容
    # print(taga.text)
    #
    # # 2/获取标签的属性
    # tagp = soup.p
    # print(tagp["class"])
    # print(tagp.attrs["class"])

    # # 3/获取标签中的内容
    # tagp = soup.p
    # print(tagp.text)
    # print(tagp.string)
    # print(list(tagp.strings))

    # # 4/嵌套选择元素
    # title = soup.head.title
    # print(title)

    # # 5/选择子节点,返回迭代器
    # child = soup.p.children
    # # print(child)
    # print(list(child))
    # 选择字节点,返回列表类型
    # contents = soup.p.contents
    # print(contents)

    # # 6/选择父元素
    # parent = soup.p.parent
    # print(parent)
    # # 逐层去找父元素
    # parents = soup.p.parents
    # # print(parents)
    # print(list(parents))

    # 7.查找兄弟元素
    sibling = soup.a.next_sibling
    print(sibling)

    sibling = soup.a.previous_sibling
    print(sibling)


